﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.IO;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.Applications.City.Entity;
using FluorineFx.IO;
using XingFuStudio.Pioneer.Applications.City.Config;
using XingFuStudio.Pioneer.Applications.City.Element;
using XingFuStudio.Pioneer.Applications.City.Config.ConfigKeys;

namespace XingFuStudio.Pioneer.Applications.City.Operation {
    /// <summary>
    /// 油井相关操作
    /// </summary>
    public class Oilwell : OPBase{
        /// <summary>
        /// 调用方法的目标类
        /// </summary>
        private readonly string Target = "CCityOilwell.callfunc";

        /// <summary>
        /// 创建业务对象
        /// </summary>
        /// <param name="httpManager"></param>
        /// <param name="account"></param>
        /// <param name="city"></param>
        /// <param name="config"></param>
        public Oilwell(HttpManager httpManager, AccountInfo account, CityInfo city, UserConfigSection config)
            : base(httpManager, account, city, config) {
        }

        /// <summary>
        /// 重写操作方法
        /// </summary>
        public override void Operate() {
            base.Operate();

            //初始化值为Null
            object value = null;
            //初始化字典是否有值标识
            bool hasValue = false;

            string message = String.Empty;

            //获取全部商业元素
            var oes = this.city.Elements.Where(s => s is OilWellInfo);
            //遍历商业元素
            foreach (OilWellInfo element in oes) {
                try {
                    //判断是否有值，并获取是否自动收获属性值
                    hasValue = config.ConfigItems.TryGetValue(OilwellConfigKeys.AutoHarvest, out value);
                    //判断配置文件中是否设置为自动收获
                    if (hasValue && Convert.ToBoolean(value)) {
                        if (city.OilwellStatus == ProductStatus.Complete && this.city.Energy > 0) {
                            //接收获物
                            bool result = this.HarvestOilwell(out message);
                            //打印日志
                            this.DoPrintLog(true, message);

                            city.OilwellStatus = ProductStatus.Waiting;
                        }
                    }

                    //判断是否有值，并获取是否自动生产属性值
                    hasValue = config.ConfigItems.TryGetValue(OilwellConfigKeys.AutoProduct, out value);
                    //判断配置文件中是否设置为自动收获
                    if (hasValue && Convert.ToBoolean(value)) {
                        if (city.OilwellStatus == ProductStatus.Waiting) {
                            //判断是否有值，并获取是否自动生产属性值
                            hasValue = config.ConfigItems.TryGetValue(OilwellConfigKeys.ProductionId, out value);
                            if (hasValue) {
                                //生产
                                bool result = this.PlachOilwell(Convert.ToInt32(value), out message);
                                //打印日志
                                this.DoPrintLog(true, message);
                            }
                        }
                    }
                } catch (Exception ex) {
                    this.DoError(ex);
                }
            }
        }

        /// <summary>
        /// 获取油产品列表
        /// </summary>
        /// <returns></returns>
        public List<ProductionInfo> GetOilProductions() {
            //初始化列表对象
            List<ProductionInfo> productions = new List<ProductionInfo>();

            //获取全部商业元素
            var oes = this.city.Elements.First(s => s is OilWellInfo);
            if (oes != null) {
                //获取AMF数据
                AMFMessage message = this.GetAMFMessage(this.Target, "getList", oes.OrderId);

                //解析数据
                Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
                Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

                //获取数据是否成功的状态
                bool result = (string)data["result"] == "succ";
                if (result) {
                    object[] list = (object[])data["list"];

                    //遍历数据
                    foreach (Dictionary<string, object> item in list) {
                        //初始化产品对象
                        ProductionInfo info = new ProductionInfo();

                        //赋值
                        info.Cash = Convert.ToInt32((string)item["cash"]);
                        info.Goods = Convert.ToInt32((string)item["goods"]);
                        info.Locked = String.IsNullOrEmpty((string)item["islocked"]) ? false : true;
                        info.PID = Convert.ToInt32((string)item["oid"]);
                        info.PName = (string)item["oname"];
                        info.PTimes = Convert.ToInt32((string)item["otime"]);

                        productions.Add(info);
                    }
                }
            }

            return productions;
        }

        /// <summary>
        /// 收获油产品
        /// </summary>        
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool HarvestOilwell(out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage(this.Target, "finishOrder");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取数据是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            if (result) {
                //解析经验，金钱，体力等信息
                int goods = Convert.ToInt32(data["goods"]);

                this.city.Goods = this.city.Goods + goods;
                this.city.Energy = this.city.Energy - 1;
                resultMessage += String.Format(" 货物增加{0}余量{1} 体力剩余{2}", 
                    goods, this.city.Goods, this.city.Energy);

            }

            return result;
        }

        /// <summary>
        /// 生厂油产品
        /// </summary>        
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool PlachOilwell(int pid, out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage(this.Target, "placeOrder", (double)pid);

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取数据是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            return result;
        }
    }
}
